﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Model;
using System.Data.SqlClient;

namespace DAL
{
    public class StagingdbTableDAL: DB
    {
        public StagingdbTableDAL(LoginInfo info)
        {
            base.loginInfo = info;
        } 
        public IList<Staging_dbTable> GetStging_dbInfo(IDataParameter[] queryParas)
        {
            IList<Staging_dbTable> list = new List<Staging_dbTable>();
            try
            {
                //创建数据库对象
                HIDB.dbo con = base.conn();
                DataTable dt = con.GetDataTable(@"select v.GUID,v.SourceSystemID,v.MDP_ViewName as ViewName,v.DisplayName,v.ViewChnName,s.SourceSysName,v.ViewName as VN 
	from [Working_db].ODS_STG_Config o left join [Working_db].[ETL_Source_ViewConfig] v on
	o.STG_TB_Name=v.MDP_ViewName inner join [Working_db].[ETL_SourceSystem] s on
	s.GUID=v.SourceSystemID where o.ODS_ID=@modelID", queryParas);
                list = Common.DBConvert.DataTableToList<Staging_dbTable>(dt);
            }
            catch (Exception)
            {
                throw;
            }
            return list;
        }

        public IList<Staging_dbTable> GetList(IDataParameter[] queryParas)
        {
            IList<Staging_dbTable> list = new List<Staging_dbTable>();
            try
            {
                //创建数据库对象
                HIDB.dbo con = base.conn();
                DataTable dt = con.StoredDataTable("[Working_db].[UP_GetStging_dbTbList]", queryParas);
                list = Common.DBConvert.DataTableToList<Staging_dbTable>(dt);
            }
            catch (Exception)
            {
                throw;
            }
            return list;
        }

        public int GetPageIndex(string guid, int pageSize)
        {
            int result = 1;
            HIDB.dbo con = base.conn();
            //添加输入输出参数
            SqlParameter[] sqlparams = new SqlParameter[] 
            { 
                new SqlParameter("@ODS_ID",guid)
                ,new SqlParameter("@CurrentPageSize",pageSize)
            };
            DataTable dt = con.StoredDataTable("[Working_db].[UP_GetStging_dbTbListPageIndex]", sqlparams);
            result = int.Parse(dt.Rows[0][0].ToString());
            return result;
        }


        public StagingdbTable Load(string id)
        {
            HIDB.dbo con = base.conn();
            string sql = @"SELECT cast(t.id as nvarchar(100)) as id,t.stgid as StgID,t.SchemaName,t.TableName,t.ChnName
	                          , cast ((select top 1 x.id from [Working_db].[GetStging_dbTbList] x where x.tablename=t._parentid1) as nvarchar(100)) _parentId 
	                         FROM [Working_db].[GetStging_dbTbList] t
                             where id = " + id;
            DataTable dt = con.GetDataTable(sql);
            IList<StagingdbTable> list = Common.DBConvert.DataTableToList<StagingdbTable>(dt);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            return new StagingdbTable();
        }

        public IList<StagingdbTable> GetODSStgTabs(string guid)
        {
            HIDB.dbo con = base.conn();
            string sql = @"SELECT cast(t.id as nvarchar(100)) as id,t.stgid as StgID,t.SchemaName,t.TableName,t.ChnName
                                    , cast ((select top 1 x.id from [Working_db].[GetStging_dbTbList] x where x.tablename=t._parentid1) as nvarchar(100)) _parentId 
                                    FROM [Working_db].[GetStging_dbTbList] t
                                    where  TableName in (select STG_TB_Name from Working_db.ODS_STG_Config where ODS_ID ='" + guid + "')";
            DataTable dt = con.GetDataTable(sql);
            IList<StagingdbTable> list = Common.DBConvert.DataTableToList<StagingdbTable>(dt);
            return list;
        }

        public int GetListCount()
        {
            int result = 0;
            try
            {
                //创建数据库对象
                HIDB.dbo con = base.conn();
                DataTable dt = con.StoredDataTable("[Working_db].[UP_GetStging_dbTbListCount]");
                result = Int32.Parse(dt.Rows[0]["RowNum"].ToString());
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
    }
}
